﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Geo_Meet.Resources;
using System.Threading.Tasks;
using Windows.Devices.Geolocation;
using Microsoft.Phone.Maps.Toolkit;
using Microsoft.Phone.Maps.Controls;
using System.Device.Location;
using System.Windows.Shapes;
using System.Windows.Media;

namespace Geo_Meet
{
    public partial class MainPage : PhoneApplicationPage
    {

        // Constructor
        public MainPage()
        {

            InitializeComponent();
            getLocation();
            

            //Map userMap = new Map();
            //userMap.Center = new GeoCoordinate();
            //userMap.ZoomLevel = 5;
            //ContentPanel.Children.Add(userMap);

            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
        private async void getLocation()
        {
            //Map userMap = new Map();
            //HyperlinkButton checkGroups = new HyperlinkButton();

            MapLayer maplayer = new MapLayer();

            Ellipse pinpoint = new Ellipse();
            pinpoint.Fill = new SolidColorBrush(Colors.Red);
            pinpoint.Height = 20;
            pinpoint.Width = 20;
            pinpoint.Opacity = 50;

            MapOverlay userLocation = new MapOverlay();
            userLocation.Content = pinpoint;
            userLocation.PositionOrigin = new Point(0.5, 0.5);

            Geolocator geolocator = new Geolocator();
            geolocator.DesiredAccuracyInMeters = 50;
            

            try
            {
                List<chatGroups> availableChatGroups = await App.MobileService.GetTable<chatGroups>().ToListAsync();
                foreach (chatGroups c in availableChatGroups)
                {
                    double lat = c.latitudE;
                    double longit = c.longitudE;
                    string chatName = c.Name;

                    Pushpin pins = new Pushpin();
                    MapOverlay chatLocation = new MapOverlay();
                    pins.Content = chatName;
                    pins.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(openChat);
                    chatLocation.Content = pins;
                    //chatLocation.PositionOrigin = new Point(0.5, 0.5);

                    //chatLocation.GeoCoordinate = new GeoCoordinate(6.4500, 3.3833);
                    chatLocation.GeoCoordinate = new GeoCoordinate(lat, longit);

                    maplayer.Add(chatLocation);


                }
                Geoposition geoposition = await geolocator.GetGeopositionAsync( );

            
            this.userMap.Center = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);
            this.userMap.ZoomLevel = 10;
               
            userLocation.GeoCoordinate = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);

            
            maplayer.Add(userLocation);
            userMap.Layers.Add(maplayer); 
                
            
            //ContentPanel.Children.Add(userMap);


            }
            catch (Exception ex)
            {
 
            }
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Createchat.xaml", UriKind.Relative));
        }

        private void openChat(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var point = e.GetPosition(this.userMap);
            var location = userMap.ConvertViewportPointToGeoCoordinate(point);
            double longit1 = location.Latitude;
            double lat1 = location.Longitude;
            NavigationService.Navigate(new Uri("/Chat.xaml", UriKind.RelativeOrAbsolute));



        }
        
        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}